package com.newbingo.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.newbingo.dao.imple.IAgentPerviewDao;
import com.newbingo.entity.AgentPerview;


public class AgentPerviewDao extends HibernateDaoSupport implements IAgentPerviewDao {
	private static final Log log=LogFactory.getLog(AgentPerviewDao.class);
	
	
	/* 
	 * 
	 */
	public void save(AgentPerview instance){
		log.debug("save AgentPerview instance");
		try{
			getSession().save(instance);
		}catch(RuntimeException re){
			log.error("save failed");
			throw re;
		}
	}
	
//	public void delete(AgentPerview instance){
//		log.debug("delete AgentPerview instance");
//		try{
//			String hql="delete from AgentPerview ap inner join ap.agent p where p.uno=";
//		}catch(RuntimeException re){
//			log.error("");
//			throw re;
//		}
//	}
	
	/* 
	 * 
	 */
	public void deleteByAgentId(Integer agId){
		log.debug("delete AgentPerview instances by ");
	}
	
	/* 
	 * 
	 */
	public AgentPerview findById(Integer id){
		log.debug("find AgentPerview instance by id");
		try{
			AgentPerview ap=(AgentPerview)getSession().get(AgentPerview.class, id);
			return ap;
		}catch(RuntimeException re){
			log.error("find failed");
			throw re;
		}
	}
	
//	public AgentPerview findByAgentIdAndPerviewId(Integer agentId,Integer perviewId){
//		log.debug("find AgentPerview instance by agentId and perviewId");
//		try{
//			Criteria cri=getSession().createCriteria(AgentPerview.class);
//			cri.createAlias("agent", "agent");
//			cri.createAlias("perview","perview");
//			cri.add(Expression.eq("agent.uno", agentId));
//			cri.add(Expression.eq("perview.id", perviewId));
//			cri.setResultTransformer(Criteria.ROOT_ENTITY);
//			List<AgentPerview> result=cri.list();
//			
//		}catch(RuntimeException re){
//			log.error("find failed");
//			throw re;
//		}
//	}
	
	/* 
	 * 
	 */
	public List<AgentPerview> findByAgentId(Integer agentId){
		log.debug("find AgentPerview instances by agent id");
		try{
			Criteria cri=getSession().createCriteria(AgentPerview.class);
			cri.createAlias("agent", "agent");
			cri.add(Expression.eq("agent.uno", agentId));
			cri.setResultTransformer(Criteria.ROOT_ENTITY);
			List<AgentPerview> result=cri.list();
			return result;
		}catch(RuntimeException re){
			log.error("find failed");
			throw re;
		}
	}
	
	
}
